Skip to content

Fix logging middleware body reading and register extra params globally#53

Merged
clavery merged 3 commits intomainfrom
fix/logging-middleware-body-read
Jan 15, 2026
Merged

Fix logging middleware body reading and register extra params globally#53
clavery merged 3 commits intomainfrom
fix/logging-middleware-body-read

Conversation

@clavery
Copy link
Copy Markdown
Collaborator

@clavery clavery commented Jan 15, 2026

Summary

Two fixes for HTTP middleware:

  1. Fix body reading error - Fixes "Body is unusable: Body has already been read" error when API returns non-JSON error responses (e.g., Cloudflare 403 HTML pages)

  2. Register extra params globally - Fixes SFCC_EXTRA_HEADERS, SFCC_EXTRA_QUERY, and SFCC_EXTRA_BODY not being applied to requests. The flags were parsed but never registered as middleware.

Fix 1: Body Reading

The logging middleware was calling response.json() and falling back to response.text() on the same cloned response. The Fetch API body can only be read once - even when json() fails to parse, it still consumes the stream.

Solution: Read as text() first, then try JSON.parse() on the string.

Fix 2: Extra Params Registration

The --extra-query, --extra-body, and --extra-headers flags (and their SFCC_* env vars) were parsed by getExtraParams() but never applied to HTTP clients.

Solution: Register the extra params middleware in globalMiddlewareRegistry during BaseCommand.init(). All HTTP clients (WebDAV, OCAPI, SLAS, ODS, MRT, Custom APIs) already check this registry.

Test plan

  • Existing middleware tests pass
  • Base command tests updated and pass
  • Test with Cloudflare-blocked endpoint to verify:
    • Error message is displayed correctly (not "Body has already been read")
    • Extra headers from SFCC_EXTRA_HEADERS appear in request logs

The logging middleware was calling response.json() and falling back to
response.text() on the same cloned response. This fails when json()
fails to parse (e.g., HTML error pages from Cloudflare) because json()
consumes the body stream even when parsing fails.

Fix: Read as text() first, then try JSON.parse() on the string. This
only consumes the body once and handles non-JSON responses correctly.
The extra params (--extra-query, --extra-body, --extra-headers) were being
parsed but never applied to requests. This fix registers the extra params
middleware in the global middleware registry during BaseCommand.init(),
so it applies to ALL HTTP clients (WebDAV, OCAPI, SLAS, ODS, MRT, Custom APIs).
@clavery clavery changed the title Fix body reading in logging middleware for non-JSON responses Fix logging middleware body reading and register extra params globally Jan 15, 2026
- Skip adding body to GET/HEAD requests which don't allow request bodies
- Clean up global middleware registry between tests to prevent leakage
- Fix query params to use modifiedRequest instead of original request
@clavery
Copy link
Copy Markdown
Collaborator Author

clavery commented Jan 15, 2026

skipping review to get e2e tests working.

@clavery clavery merged commit 20b8db5 into main Jan 15, 2026
3 checks passed
@clavery clavery deleted the fix/logging-middleware-body-read branch January 27, 2026 01:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant